﻿using AutoMapper;
using Microsoft.Extensions.DependencyInjection;
using Google.Protobuf.WellKnownTypes;
using Proto2Entity.Dtos;
using System;
using System.Collections.Generic;
using protobufConvertor;
using System.Diagnostics;

namespace Proto2Entity
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            #region  mapper 第一个demo


            //var services = new ServiceCollection();

            //var config = new MapperConfiguration(cfg =>
            //{
            //    //    cfg.CreateMap<PaymentInfoProto, PaymentInfoEntity>()
            //    //    .ForMember(d => d.CreateTime, opt => opt.MapFrom(src => src.CreateTime.ToDateTime()))
            //    //    ;
            //    //    cfg.CreateMap<PaymentInfoEntity, PaymentInfoProto>().ForMember(d => d.CreateTime, opt => opt.MapFrom(src => src.CreateTime.ToUniversalTime().ToTimestamp()));

            //    //    cfg.CreateMap<ProductInfoEntity, ProductInfoProto>()

            //    //    //.ForMember(d => d.ProductId, opt => opt.MapFrom(src => src.ProductId))
            //    //    //.ForMember(d => d.CreateTime, opt => opt.MapFrom(src => src.CreateTime.ToDateTime()))
            //    //    ;
            //    //    cfg.CreateMap<ProductInfoProto, ProductInfoEntity>()
            //    //.ForMember(d => d.CreateTime, opt => opt.MapFrom(src => src.CreateTime.ToUniversalTime().ToTimestamp()))

            //    cfg.CreateMap<PaymentInfoProto, PaymentInfoEntity>();
            //    cfg.CreateMap<PaymentInfoEntity, PaymentInfoProto>();

            //    cfg.CreateMap<ProductInfoEntity, ProductInfoProto>();
            //    cfg.CreateMap<ProductInfoProto, ProductInfoEntity>();

            //    cfg.CreateMap<Timestamp, DateTime>().ConvertUsing(new ProtoTimeToCSharpConverter());
            //cfg.CreateMap<DateTime, Timestamp>().ConvertUsing(new CSharpToProtoTimeConverter());


            //});

            //IMapper mapper = config.CreateMapper();

            //PaymentInfoProto paymentInfoProto = new PaymentInfoProto
            //{
            //    Amount = 100.1f,
            //    CreateTime = DateTime.UtcNow.AddDays(1).ToTimestamp(),
            //    Id = 12,
            //    PaymentMethod = (int)PaymentMethod.Alipay,
            //    PaymentRecordId = Guid.NewGuid().ToString()

            //};

            //var productProtoList  = new List<ProductInfoProto>()
            //{
            //    new ProductInfoProto(){
            //        Count =11,
            //        Id=12,
            //        ProductId=Guid.NewGuid().ToString(),
            //        ProductName = "花生",
            //        TotalPrice=86,

            //    },
            //    new ProductInfoProto(){
            //        Count =3,
            //        Id=14,
            //        ProductId=Guid.NewGuid().ToString(),
            //        ProductName = "瓜子",TotalPrice=16,
            //    }

            //};


            //paymentInfoProto.ProductInfoList.Add(productProtoList);
            //var paymentInfoentityList = mapper.Map<PaymentInfoEntity>(paymentInfoProto);
            #endregion

            var service = new ServiceCollection();
            service.UseAutoMapper();
            service.AddSingleton<IService, DemoService>();
            var builder = service.BuildServiceProvider();
            var demoService = builder.GetService<IService>();
            PaymentInfoEntity obj = null;
            var stopwatch = Stopwatch.StartNew();
            for (var i = 0; i < 1000000; i++)
            {
                obj = demoService.TestConvert();
            }
            Console.WriteLine(stopwatch.ElapsedMilliseconds);


            Console.Read();


        }

      
    }
    public interface IService
    {
        PaymentInfoEntity TestConvert();
    }
    public class DemoService : IService
    {
        private IMapper mapper;
        public DemoService(IMapper mapper)
        {
            this.mapper = mapper;

        }

        public PaymentInfoEntity TestConvert()
        {
            PaymentInfoProto paymentInfoProto = new PaymentInfoProto
            {
                Amount = 100.1f,
                CreateTime = DateTime.UtcNow.AddDays(1).ToTimestamp(),
                Id = 12,
                PaymentMethod = (int)PaymentMethod.Alipay,
                PaymentRecordId = Guid.NewGuid().ToString()

            };

            var productProtoList = new List<ProductInfoProto>()
                {
                    new ProductInfoProto(){
                        Count =11,
                        Id=12,
                        ProductId=Guid.NewGuid().ToString(),
                        ProductName = "花生",
                        TotalPrice=86,

                    },
                    new ProductInfoProto(){
                        Count =3,
                        Id=14,
                        ProductId=Guid.NewGuid().ToString(),
                        ProductName = "瓜子",TotalPrice=16,
                    }

                };
            paymentInfoProto.ProductInfoList.Add(productProtoList);
            var entity = mapper.Map<PaymentInfoEntity>(paymentInfoProto);
            var newProto = mapper.Map<PaymentInfoProto>(entity);


            return entity;

        }


    }

    public class MyProfile : Profile
    {
        public MyProfile()
        {
            CreateMap<PaymentInfoProto, PaymentInfoEntity>();
            CreateMap<PaymentInfoEntity, PaymentInfoProto>();

            CreateMap<ProductInfoEntity, ProductInfoProto>();
            CreateMap<ProductInfoProto, ProductInfoEntity>();
        }
    }
}
